package mortal.game.resource
{
	import com.gengine.resource.*;
	import flash.utils.*;
	
	public class ExperienceConfig extends Object
	{
		private var _map:Dictionary;
		private var _levelMap:Dictionary;
		private static var _instance:ExperienceConfig;
		
		public function ExperienceConfig()
		{
			this._map = new Dictionary();
			this._levelMap = new Dictionary();
			if (_instance != null)
			{
				throw new Error(" ResConfig 单例 ");
			}
			this.init();
			
		}
		
		private function write(exp:Object) : void
		{
			var item:Object = null;
			for each (item in exp)
			{
				
				this._map[item.level] = item.upgradeNeedExperience;
			}
			
		}
		
		public function init() : void
		{
			var json:* = ConfigManager.instance.getJSONByFileName("t_experience.json");
			this.write(json);
			
		}
		
		public function getValueByLevel(exp:int) : Number
		{
			return this._map[exp];
		}
		
		public function getTotalByLevel(exp:int) : Number
		{
			var total:Number = 0;
			var type:String = null;
			if (this._levelMap[exp] == null)
			{
				total = 0;
				for (type in this._map)
				{
					
					if (int(type) < exp)
					{
						total = total + int(this._map[type]);
					}
				}
				this._levelMap[exp] = total;
			}
			
			return this._levelMap[exp];
		}
		
		public function getLevelByExp(exp:Number) : int
		{
			var tlevel:Number = 0;
			var low:int = 0;
			var total:int = 100;
			while (total > low)
			{
				
				tlevel = this.getTotalByLevel((total - 1));
				if (exp >= tlevel)
				{
					return total;
				}
				total = total - 1;
			}
			return total;
		}
		
		public static function get instance() : ExperienceConfig
		{
			if (_instance == null)
			{
				_instance = new ExperienceConfig;
			}
			return _instance;
		}
		
	}
}
